package com.attendance.www.hr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.DepartmentBean;
import com.attendance.www.hr.beans.EmployeeBean;
import com.attendance.www.hr.beans.ShiftPermissionBean;
import com.attendance.www.hr.beans.ShiftPermissionPageBean;
import com.attendance.www.hr.dao.mapper.CorporationMapper;
import com.attendance.www.hr.dao.mapper.DepartmentMapper;
import com.attendance.www.hr.dao.mapper.EmployeeMapper;
import com.attendance.www.hr.dao.mapper.ShiftDepartmentMapper;
import com.attendance.www.hr.dao.mapper.ShiftEmployeeMapper;
import com.attendance.www.hr.dao.mapper.ShiftPermissionExtMapper;
import com.attendance.www.hr.dao.mapper.ShiftPermissionMapper;
import com.attendance.www.hr.entitys.Corporation;
import com.attendance.www.hr.entitys.CorporationExample;
import com.attendance.www.hr.entitys.Department;
import com.attendance.www.hr.entitys.DepartmentExample;
import com.attendance.www.hr.entitys.Employee;
import com.attendance.www.hr.entitys.EmployeeExample;
import com.attendance.www.hr.entitys.ShiftDepartmentExample;
import com.attendance.www.hr.entitys.ShiftDepartmentKey;
import com.attendance.www.hr.entitys.ShiftEmployeeExample;
import com.attendance.www.hr.entitys.ShiftEmployeeKey;
import com.attendance.www.hr.entitys.ShiftPermission;
import com.attendance.www.hr.entitys.ShiftPermissionExample;
import com.attendance.www.hr.service.IShiftPermissionService;

@Service
public class ShiftPermissionServiceImpl implements IShiftPermissionService {

	@Resource
	private ShiftPermissionMapper shiftPermissionDao;
	@Resource
	private ShiftPermissionExtMapper shiftPermissionExtDao;
	@Resource
	private ShiftEmployeeMapper shiftEmployeeDao;
	@Resource
	private ShiftDepartmentMapper shiftDepartmentDao;
	@Resource
	private EmployeeMapper employeeDao;
	@Resource
	private DepartmentMapper departmentDao;
	@Resource
	private CorporationMapper corporationDao;

	/**
	 * 获取列表带分页
	 */
	@Override
	public ShiftPermissionPageBean getShiftPermissionList(ShiftPermissionBean shiftPermissionBean, Integer startNum,
			Integer pageCount) {
		List<ShiftPermissionBean> shiftPermissionBeanList = shiftPermissionExtDao
				.getShiftPermissionList(shiftPermissionBean, startNum, pageCount);
		Integer pageTotal = shiftPermissionExtDao.getShiftPermissionTotal(shiftPermissionBean);

		// 获取公司
		CorporationExample corporationExample=new CorporationExample();
		corporationExample.createCriteria().andIsDeleteEqualTo(0);
		List<Corporation> corporationList=corporationDao.selectByExample(corporationExample);
		
		// 循环设置人员和部门信息
		for (int i = 0; i < shiftPermissionBeanList.size(); i++) {
			ShiftPermissionBean spb = shiftPermissionBeanList.get(i);
			// 获取班次人员关系
			ShiftEmployeeExample shiftEmployeeExample = new ShiftEmployeeExample();
			shiftEmployeeExample.createCriteria().andShiftIdEqualTo(spb.getShiftId());
			List<ShiftEmployeeKey> shiftEmployeeKeyList = shiftEmployeeDao.selectByExample(shiftEmployeeExample);
			if (shiftEmployeeKeyList.size() > 0) {
				// 获取人员集合
				List<Integer> employeeIdList = shiftEmployeeKeyList.stream().map(ShiftEmployeeKey::getEmployeeId)
						.collect(Collectors.toList());
				if (employeeIdList.size() > 0) {
					// 根据人员集合查询
					EmployeeExample employeeExample = new EmployeeExample();
					employeeExample.createCriteria().andEmployeeIdIn(employeeIdList);
					employeeExample.setOrderByClause("employee_no asc");
					List<Employee> employeeList = employeeDao.selectByExample(employeeExample);
					List<EmployeeBean> employeeBeanList = new ArrayList<EmployeeBean>();
					for (int ei = 0; ei < employeeList.size(); ei++) {
						Employee employee = employeeList.get(ei);
						EmployeeBean employeeBean = new EmployeeBean();
						employeeBean.setEmployeeId(employee.getEmployeeId());
						employeeBean.setEmployeeNo(employee.getEmployeeNo());
						employeeBean.setChineseName(employee.getChineseName());
						employeeBean.setEnglishName(employee.getEnglishName());
						employeeBeanList.add(employeeBean);
					}
					// 添加到班次人员数据
					spb.setEmployeeList(employeeBeanList);
				}
			}

			// 获取班次部门关系
			ShiftDepartmentExample shiftDepartmentExample = new ShiftDepartmentExample();
			shiftDepartmentExample.createCriteria().andShiftIdEqualTo(spb.getShiftId());
			List<ShiftDepartmentKey> shiftDepartmentKeyList = shiftDepartmentDao
					.selectByExample(shiftDepartmentExample);
			if (shiftDepartmentKeyList.size() > 0) {
				// 获取部门集合
				List<Integer> departmentIdList = shiftDepartmentKeyList.stream()
						.map(ShiftDepartmentKey::getDepartmentId).collect(Collectors.toList());
				if (departmentIdList.size() > 0) {
					// 根据部门集合查询
					DepartmentExample departmentExample = new DepartmentExample();
					departmentExample.createCriteria()
					.andDepartmentIdIn(departmentIdList)
					.andIsDisableEqualTo(0);
					departmentExample.setOrderByClause("department_code asc");
					List<Department> departmentList = departmentDao.selectByExample(departmentExample);
					List<DepartmentBean> departmentBeanList = new ArrayList<DepartmentBean>();
					for (int di = 0; di < departmentList.size(); di++) {
						Department department = departmentList.get(di);
						DepartmentBean departmentBean = new DepartmentBean();
						departmentBean.setDepartmentId(department.getDepartmentId());
						departmentBean.setDepartmentCode(department.getDepartmentCode());
						departmentBean.setDepartmentName(department.getDepartmentName());
						// 获取公司
						List<Corporation> cList = corporationList.stream().filter(s -> s.getCorporationId() == department.getCorporationId()).collect(Collectors.toList());
						if(cList!=null && cList.size()>0){
							departmentBean.setCorporationCode(cList.get(0).getCorporationCode());
							departmentBean.setCorporationName(cList.get(0).getCorporationName());
						}
						departmentBeanList.add(departmentBean);
					}
					// 添加到班次部门数据
					spb.setDepartmentList(departmentBeanList);
				}
			}
			shiftPermissionBeanList.set(i, spb);
		}

		ShiftPermissionPageBean shiftPermissionPageBean = new ShiftPermissionPageBean();
		shiftPermissionPageBean.setShiftPermissionBeanList(shiftPermissionBeanList);
		shiftPermissionPageBean.setPageTotal(pageTotal);
		return shiftPermissionPageBean;
	}

	/**
	 * 验收是否存在
	 */
	@Override
	public boolean getVerifyShiftPermission(ShiftPermissionBean shiftPermissionBean) {
		ShiftPermissionExample example = new ShiftPermissionExample();
		ShiftPermissionExample.Criteria criteria = example.createCriteria();
		criteria.andShiftNameEqualTo(shiftPermissionBean.getShiftName());
		if (shiftPermissionBean.getShiftId() != null) {
			criteria.andShiftIdNotEqualTo(shiftPermissionBean.getShiftId());
		}
		criteria.andIsDeleteEqualTo(0);
		List<ShiftPermission> shiftPermissionList = shiftPermissionDao.selectByExample(example);
		if (shiftPermissionList.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 添加
	 */
	@Override
	public boolean saveShiftPermission(ShiftPermissionBean shiftPermissionBean) {
		ShiftPermission shiftPermission = new ShiftPermission();
		shiftPermission.setShiftName(shiftPermissionBean.getShiftName());
		int record = shiftPermissionDao.insertSelective(shiftPermission);

		// 添加人员
		List<EmployeeBean> employeeBeanList = shiftPermissionBean.getEmployeeList();
		if (employeeBeanList != null) {
			// 循环保存数据
			for (int i = 0; i < employeeBeanList.size(); i++) {
				EmployeeBean employeeBean = employeeBeanList.get(i);
				ShiftEmployeeKey shiftEmployeeKey = new ShiftEmployeeKey();
				shiftEmployeeKey.setShiftId(shiftPermission.getShiftId());
				shiftEmployeeKey.setEmployeeId(employeeBean.getEmployeeId());
				shiftEmployeeDao.insertSelective(shiftEmployeeKey);
			}
		}

		// 添加部门
		List<DepartmentBean> departmentBeanList = shiftPermissionBean.getDepartmentList();
		if (departmentBeanList != null) {
			// 循环保存数据
			for (int j = 0; j < departmentBeanList.size(); j++) {
				DepartmentBean departmentBean = departmentBeanList.get(j);
				ShiftDepartmentKey shiftDepartmentKey = new ShiftDepartmentKey();
				shiftDepartmentKey.setShiftId(shiftPermission.getShiftId());
				shiftDepartmentKey.setDepartmentId(departmentBean.getDepartmentId());
				shiftDepartmentDao.insertSelective(shiftDepartmentKey);
			}
		}

		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 修改
	 */
	@Override
	public boolean updateShiftPermission(ShiftPermissionBean shiftPermissionBean) {
		ShiftPermission shiftPermission = new ShiftPermission();
		shiftPermission.setShiftId(shiftPermissionBean.getShiftId());
		shiftPermission.setShiftName(shiftPermissionBean.getShiftName());
		shiftPermission.setUpdateTime(new Date());
		int record = shiftPermissionDao.updateByPrimaryKeySelective(shiftPermission);

		// 添加人员
		List<EmployeeBean> employeeBeanList = shiftPermissionBean.getEmployeeList();
		if (employeeBeanList != null) {
			// 先删除后添加
			ShiftEmployeeExample seExample = new ShiftEmployeeExample();
			seExample.createCriteria().andShiftIdEqualTo(shiftPermissionBean.getShiftId());
			shiftEmployeeDao.deleteByExample(seExample);

			// 循环保存数据
			for (int i = 0; i < employeeBeanList.size(); i++) {
				EmployeeBean employeeBean = employeeBeanList.get(i);
				ShiftEmployeeKey shiftEmployeeKey = new ShiftEmployeeKey();
				shiftEmployeeKey.setShiftId(shiftPermission.getShiftId());
				shiftEmployeeKey.setEmployeeId(employeeBean.getEmployeeId());
				shiftEmployeeDao.insertSelective(shiftEmployeeKey);
			}
		}

		// 添加部门
		List<DepartmentBean> departmentBeanList = shiftPermissionBean.getDepartmentList();
		if (departmentBeanList != null) {
			// 先删除后添加
			ShiftDepartmentExample sdExample = new ShiftDepartmentExample();
			sdExample.createCriteria().andShiftIdEqualTo(shiftPermissionBean.getShiftId());
			shiftDepartmentDao.deleteByExample(sdExample);

			// 循环保存数据
			for (int j = 0; j < departmentBeanList.size(); j++) {
				DepartmentBean departmentBean = departmentBeanList.get(j);
				ShiftDepartmentKey shiftDepartmentKey = new ShiftDepartmentKey();
				shiftDepartmentKey.setShiftId(shiftPermission.getShiftId());
				shiftDepartmentKey.setDepartmentId(departmentBean.getDepartmentId());
				shiftDepartmentDao.insertSelective(shiftDepartmentKey);
			}
		}

		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 删除
	 */
	@Override
	public boolean deleteShiftPermission(String shiftId) {
		ShiftPermission shiftPermission = new ShiftPermission();
		shiftPermission.setShiftId(Integer.parseInt(shiftId));
		shiftPermission.setIsDelete(1);
		shiftPermission.setUpdateTime(new Date());
		int record = shiftPermissionDao.updateByPrimaryKeySelective(shiftPermission);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}
}
